﻿package nl.joeyvandijk
{
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.text.TextField;
	import flash.text.AntiAliasType;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.text.Font;
	import flash.filters.DropShadowFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BitmapFilter;
	import caurina.transitions.Tweener;
	
	public class Show extends Sprite
	{
		private const TIME_IN:Number = 2;
		private const TIME_OUT:Number = 2;
		private const TIME_RUN:Number = 6;
		private const TIME_TEXT:Number = 5;
		
		private var txt1:TextField = new TextField();
		private var txt2:TextField = new TextField();
		private var bmp1:Bitmap;
		private var bmp2:Bitmap;
		private var select:uint = 1;
		private var item:uint = 0;
		private var data:Array;
		private var pics:Array;		
		private var tf:TextFormat;
		private var xp:uint = 0;
		private var yp:uint = 0;
		private var logo:Logo;
		private var currentLink:String = '';

		public function Show()
		{
			var fnt:Font = new FontMe();
			tf = new TextFormat();
			tf.font = fnt.fontName;			
			tf.size = 14;
			tf.color = 0xffffff;
			tf.kerning = true;
			tf.letterSpacing = 1;
			
			create();
		}		
		
		public function start(input:Array, images:Array, lg:Logo):void
		{
			//reset
			data = input;
			pics = images;
			logo = lg;
			
			//show first image & text
			show();
			
			Tweener.removeTweens(this);
			Tweener.addTween(this,{alpha:1, time:2, transition:'easeOutExpo'});			
		}
		
		private function show():void
		{
			var bmp:Bitmap;
			var txt:TextField;
			
			if(select == 1){
				bmp = bmp1;
				txt = txt1;		
				select = 2;
			}else{
				bmp = bmp2;
				txt = txt2;
				select = 1;
			}
			
			//set xp,yp
			var rr:Array = data[item].textxy.split(',');
			xp = Number(rr[0]);
			yp = Number(rr[1]);
			
			//set link
			currentLink = data[item].link;
			
			//set logo
			var lg:Array = data[item].logoxy.split(',');
			logo.x = Number(lg[0]);
			logo.y = Number(lg[1]);
			logo.load(data[item].logo);
			
			//get bitmap
			if(bmp != null){
				if(this.contains(bmp)){
					removeChild(bmp);
				}
			}
			bmp = new Bitmap(pics[item].bitmapData);
			bmp.alpha = 0;
			bmp.x = 0
			bmp.y = 0;
			addChild(bmp);
				
			//get text
			if(txt != null)
			{
				if(this.contains(txt)){
					removeChild(txt);
				}
			}
			txt.alpha = 0;
			txt.text = data[item].text;
			txt.x = xp;
			txt.y = yp;
			addChild(txt);
				
			//animate
			var total:Number = TIME_IN+TIME_OUT+TIME_RUN;
			Tweener.removeTweens(bmp);
			Tweener.addTween(bmp,{alpha:1, time:TIME_IN, transition:'easeOutExpo'});			
			Tweener.addTween(bmp,{y:-(bmp.bitmapData.height - stage.stageHeight), time:total, transition:'easeOutSine'});
			Tweener.removeTweens(txt);
			Tweener.addTween(txt,{alpha:1, y: yp - 30,time:TIME_IN, transition:'easeInExpo', onComplete:move,onCompleteParams:[txt]});
			Tweener.addTween(bmp,{time:TIME_IN+TIME_RUN, transition:'linear', onComplete:show});
						
			//set next
			next();
		}
		
		private function move(obj:TextField):void
		{
			Tweener.removeTweens(obj);
			Tweener.addTween(obj,{y: yp - 90, time:TIME_TEXT, transition:'linear',onComplete:hide, onCompleteParams:[obj]});			
		}
		
		private function hide(obj:TextField):void
		{
			Tweener.removeTweens(obj);
			Tweener.addTween(obj,{alpha:0, y: yp - 120,time:TIME_OUT, transition:'easeOutExpo'});
		}		
		
		private function next():void
		{
			item++;
			if(item == data.length)
			{
				item = 0;
			}
		}
		private function create():void
		{
			//create textfield			
			txt1 = new TextField();
			txt1.name = name;
			txt1.selectable = false;
			txt1.embedFonts = true;
			txt1.autoSize = TextFieldAutoSize.LEFT;
			txt1.antiAliasType = AntiAliasType.ADVANCED;
			txt1.defaultTextFormat = tf;
			txt1.htmlText = '';
			txt1.x = xp;
			txt1.filters = [getBitmapFilter()];			
			addChild(txt1);
			
			txt2 = new TextField();
			txt2.name = name;
			txt2.selectable = false;
			txt2.embedFonts = true;
			txt2.autoSize = TextFieldAutoSize.LEFT;
			txt2.antiAliasType = AntiAliasType.ADVANCED;
			txt2.defaultTextFormat = tf;
			txt2.htmlText = '';
			txt2.x = xp;
			txt2.filters = [getBitmapFilter()];
			addChild(txt2);
		}
		private function getBitmapFilter():BitmapFilter {
            var color:Number = 0x000000;
            var angle:Number = 45;
            var alpha:Number = 0.8;
            var blurX:Number = 4;
            var blurY:Number = 4;
            var distance:Number = 2;
            var strength:Number = 1.45;
            var inner:Boolean = false;
            var knockout:Boolean = false;
            var quality:Number = BitmapFilterQuality.MEDIUM;
            return new DropShadowFilter(distance,
                                        angle,
                                        color,
                                        alpha,
                                        blurX,
                                        blurY,
                                        strength,
                                        quality,
                                        inner,
                                        knockout);
        }
		
		public function click():void
		{
			navigateToURL(new URLRequest(currentLink),'_blank');
		}
	}
}